package com.jzo2o.orders.manager.controller.consumer;

import cn.hutool.core.bean.BeanUtil;
import com.jzo2o.api.market.dto.response.AvailableCouponsResDTO;
import com.jzo2o.api.orders.dto.request.OrderCancelReqDTO;
import com.jzo2o.api.orders.dto.response.OrderResDTO;
import com.jzo2o.api.orders.dto.response.OrderSimpleResDTO;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.orders.manager.model.dto.OrderCancelDTO;
import com.jzo2o.orders.manager.model.dto.request.OrdersPayReqDTO;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import com.jzo2o.orders.manager.model.dto.response.OrdersPayResDTO;
import com.jzo2o.orders.manager.model.dto.response.PlaceOrderResDTO;
import com.jzo2o.orders.manager.service.IOrdersCreateService;
import com.jzo2o.orders.manager.service.IOrdersManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author itcast
 */
@Api(value = "/consumer/orders", tags = {""})
@RestController("consumerOrdersController")
@RequestMapping("/consumer/orders")
public class ConsumerOrdersController {

    @Resource
    private IOrdersManagerService ordersManagerService;

    @Resource
    private  IOrdersCreateService ordersCreateService;


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "根据订单id查询", notes = "", httpMethod = "GET")
    @GetMapping("/{id}")
    public OrderResDTO detail(@PathVariable("id") Long id) {
        return ordersManagerService.getDetail(id);
    }
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "ordersStatus", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "long", name = "sortBy", value = "")
    })
    @ApiOperation(value = "订单滚动分页查询", notes = "", httpMethod = "GET")
    @GetMapping("/consumerQueryList")
    public List<OrderSimpleResDTO> consumerQueryList(@RequestParam(value = "ordersStatus", required = false) Integer ordersStatus,
                                                     @RequestParam(value = "sortBy", required = false) Long sortBy) {
        return ordersManagerService.consumerQueryList(UserContext.currentUserId(), ordersStatus, sortBy);
    }

    /**
     * 下单接口
     * @param placeOrderReqDTO 下单请求参数
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "PlaceOrderReqDTO", name = "placeOrderReqDTO", value = "下单请求参数", required = true)
    })
    @ApiOperation(value = "下单接口", notes = "下单接口", httpMethod = "POST")
    @PostMapping("/place")
    public PlaceOrderResDTO place(@RequestBody PlaceOrderReqDTO placeOrderReqDTO){
        PlaceOrderResDTO placeOrderResDTO = ordersCreateService.placeOrder(placeOrderReqDTO);
        return placeOrderResDTO;
    }


    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "OrdersPayReqDTO", name = "ordersPayReqDTO", value = "", required = true)
    })
    @ApiOperation(value = "订单支付", notes = "", httpMethod = "PUT")
    @PutMapping("/pay/{id}")
    public OrdersPayResDTO pay(@PathVariable("id") Long id, @RequestBody OrdersPayReqDTO ordersPayReqDTO){
        OrdersPayResDTO pay = ordersCreateService.pay(id, ordersPayReqDTO);
        return pay;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "long", name = "id", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "GET")
    @GetMapping("/pay/{id}/result")
    public OrdersPayResDTO payResult(@PathVariable("id") Long id){
        OrdersPayResDTO ordersPayResDTO = ordersCreateService.getPayResultFromTradServer(id);

        return ordersPayResDTO;
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    public void cancel(@RequestBody OrderCancelReqDTO orderCancelReqDTO){
        OrderCancelDTO orderCancelDTO = BeanUtils.toBean(orderCancelReqDTO, OrderCancelDTO.class);
        //用户id
        orderCancelDTO.setCurrentUserId(UserContext.currentUserId());
        //用户名称
        orderCancelDTO.setCurrentUserName(UserContext.currentUser().getName());
        //用户类型
        orderCancelDTO.setCurrentUserType(UserContext.currentUser().getUserType());

        ordersManagerService.cancel(orderCancelDTO);

    }

    @GetMapping("/getAvailableCoupons")
    @ApiOperation("获取可用优惠券")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serveId", value = "服务id", required = true, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "purNum", value = "购买数量，默认1", required = false, dataTypeClass = Long.class)
    })
    public List<AvailableCouponsResDTO> getCoupons(@RequestParam(value = "serveId", required = true) Long serveId,
                                                   @RequestParam(value = "purNum", required = false, defaultValue = "1") Integer purNum) {
        List<AvailableCouponsResDTO> availableCoupons = ordersCreateService.getAvailableCoupons(serveId, purNum);

        return availableCoupons;
    }

}
